package com.xltx.wechat.config;

import com.xltx.wechat.properties.WechatProperties;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author liuyc
 * @ClassName WechatConfig
 * @Description
 * @Date 2022/3/16 10:41
 */
@Component
public class WechatConfig {

    @Resource
    private WechatProperties wechatProperties;

    /**
     * 微信公众号服务层 bean注册
     */
    @Bean
    public WxMpService wxMpService(){
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
        return wxMpService;
    }

    /**
     * 微信公众号配置 bean注册
     */
    @Bean
    public WxMpConfigStorage wxMpConfigStorage() {
        WxMpDefaultConfigImpl wxMpConfigStorage = new WxMpDefaultConfigImpl();
        // 设置开发者的id和密钥
        wxMpConfigStorage.setAppId(wechatProperties.getAppId());
        wxMpConfigStorage.setSecret(wechatProperties.getAppSecret());
        return wxMpConfigStorage;
    }
}
